package zq.cn.activity;

import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.List;

import adapter.GameAdapter;
import defview.MyView;
import utils.Images;
import utils.TableDao;

public class GameActivity extends AppCompatActivity {
    private LinearLayout topBgImg;
    private Button backBtn;
    private TextView topStar;
    private TextView coinImg;
    private TextView coinCount;
    private TextView leftDelete;
    private TextView leftHint;
    private TextView txtImg;
    private TextView imgTitle;
    private TextView rightImg;
    private MyView myView;
    private GridView gameGrid;
    private GameAdapter gameAdapter;

    private List<View> values = new ArrayList<>();

    private Map<String, Object> dataMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        topBgImg = (LinearLayout) findViewById(R.id.game_top_img);
        backBtn = (Button) findViewById(R.id.game_back);
        topStar = (TextView) findViewById(R.id.game_top_star);
        coinImg = (TextView) findViewById(R.id.game_coin_img);
        coinCount = (TextView) findViewById(R.id.game_coin_count);
        leftDelete = (TextView) findViewById(R.id.game_left_delete);
        leftHint = (TextView) findViewById(R.id.game_left_hint);
        txtImg = (TextView) findViewById(R.id.game_txt_img);
        imgTitle = (TextView) findViewById(R.id.game_img_title);
        rightImg = (TextView) findViewById(R.id.game_right_img);
        myView = (MyView) findViewById(R.id.game_view);
        gameGrid = (GridView) findViewById(R.id.game_grid);

        topBgImg.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getTopBackground()));
        backBtn.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getBackBtn()));
        topStar.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getTopImg()));
        coinImg.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getCoinImg()));
        leftDelete.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getLeftDelete()));
        leftHint.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getLeftHint()));
        imgTitle.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getImgTitle()));
        rightImg.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getRightImg()));


        int id = getIntent().getIntExtra("stageNum",1);
        //获取内容
        dataMap = TableDao.getDataById(id);
        //为顶部关卡设置文本
        topStar.setText(id+"");
        //设置从InitActivity中获取的金币
        coinCount.setText(String.valueOf(getSharedPreferences("game", MODE_PRIVATE).getInt("coin", 100)));
        //设置图片类型文本
        imgTitle.setText(dataMap.get("imgTitle").toString());
        //设置图片
        try {
            txtImg.setBackgroundDrawable(new BitmapDrawable(getResources(),
                    BitmapFactory.decodeStream(getAssets().open(dataMap.get("imgPath").toString()))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置网格的文字
        String allTxt = dataMap.get("allText").toString();
        List<Character> text = new ArrayList<>();
        for(int i=0;i<allTxt.length();i++) {
            text.add(allTxt.charAt(i));
        }
        //调整文字顺序
        Collections.shuffle(text);
        //给每个格子赋值
        LayoutInflater l = LayoutInflater.from(this);
        for(int i=0;i<text.size();i++) {
            View contextView = l.inflate(R.layout.stage_list, null);
            TextView listNum = (TextView) contextView.findViewById(R.id.stage_list_num);
            listNum.setText(text.get(i).toString());
            listNum.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getSelectBgImg()));
            values.add(contextView);
        }
        gameAdapter = new GameAdapter(this, values);
        gameGrid.setAdapter(gameAdapter);

        myView.initAnswer(dataMap.get("imgAnswer").toString());
        gameGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                View v = values.get(position);
                TextView tv = (TextView) v.findViewById(R.id.stage_list_num);
                myView.initUserAnswer(tv.getText().toString(), position);
                v.setVisibility(View.INVISIBLE);
            }
        });
    }

    public List<View> getValues() {
        return values;
    }
}
